<%--
   Select Selector
   Copyright (c) 2013 ISPERP
   Version : 1.0.0
 --%>
<%@tag import="org.springframework.jdbc.core.JdbcTemplate"%>
<%@tag import="java.util.Map"%>
<%@tag import="org.apache.commons.lang3.StringUtils"%>
<%@tag import="itsm.isperp.module.entity.app.AppDict"%>
<%@tag import="java.util.List"%>
<%@tag import="itsm.isperp.framework.core.context.ContextHolder"%>
<%@tag import="itsm.isperp.module.service.app.AppDictService"%>
<%@ tag language="java" pageEncoding="UTF-8"
	deferredSyntaxAllowedAsLiteral="false" trimDirectiveWhitespaces="true"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ attribute name="id" required="true" type="java.lang.String"
	description="唯一标识."%>
<%@ attribute name="selectTable" required="true" type="java.lang.String"
	description="唯一标识."%>
<%@ attribute name="hasEmptyValue" required="false"
	type="java.lang.Boolean" description="hasEmptyValue"%>
<%@ attribute name="nameColumn" required="true" type="java.lang.String"
	description="nameColumn"%>
<%@ attribute name="valueColumn" required="true" type="java.lang.String"
	description="valueColumn"%>
<%@ attribute name="selectCondition" required="false"
	type="java.lang.String" description="selectCondition"%>

<%@ attribute name="orderDescription" required="false"
	type="java.lang.String" description="orderDescription"%>

<%@ attribute name="onchangeFunc" required="false"
	type="java.lang.String" description="onchangeFunc"%>


<c:set var="hasEmptyValue"
	value="${empty hasEmptyValue?true:hasEmptyValue }"></c:set>

<jsp:doBody />
<%
	JdbcTemplate jdbcTemplate = ContextHolder
			.getSpringBean("jdbcTemplate");

	String sql = "SELECT " + valueColumn + " as valueColumn,"
			+ nameColumn + " as nameColumn FROM " + selectTable;

	if (StringUtils.isNoneEmpty(selectCondition)) {
		sql += " " + " WHERE " + selectCondition;
	}

	if (StringUtils.isNoneEmpty(orderDescription)) {
		sql += " " + " ORDER BY  " + orderDescription;
	}

	List<Map<String, Object>> result = null;

	try {
		result = jdbcTemplate.queryForList(sql);
	} catch (Exception ex) {
	}
%>
<select id="${id }" onchange="${onchangeFunc }"
	data-allow-empty="${hasEmptyValue }" op="${op }">
	<%
		if (result != null) {
			for (Map<String, Object> item : result) {
	%>
	<option value="<%=item.get("valueColumn")%>"><%=item.get("nameColumn")%></option>
	<%
		}
		}
	%>
</select>
